In [ ]:
# Load code to setup authorization for an IPython Notebook. Note that this is a temporary step
# that is required until the Earth Engine Python API is updated to include this logic.
%run 'authorize_earth_engine_in_notebook.ipynb'
# Initialize Earth Engine
# Note that we are calling a function defined in the previously run IPython Notebook, rather than
# the typical call to ee.Initialize()
ee_initialize()
In [ ]:
%run 'define_google_maps_interactive_widget.ipynb'
In [ ]:
map = GoogleMapsWidget(lat=31.073, lng=30.732, zoom=8)
display(map)
composite = ee.Image("L7_TOA_1YEAR_2000")
bands = ["10", "20", "30", "40", "50", "62", "70"]
# Use the MCD12 land-cover as training data.
cover = ee.Image("MCD12Q1/MCD12Q1_005_2001_01_01").select("Land_Cover_Type_1")
# A region of the image to train with.
region = ee.Geometry.Rectangle(30, 30, 32.5, 31.6)
PALETTE = ','.join([
'aec3d4', # water
'152106', '225129', '369b47', '30eb5b', '387242', # forest
'6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40', # shrub, grass, savanah
'111149', # wetlands
'8dc33b', # croplands
'cc0013', # urban
'6ca80d', # crop mosaic
'd7cdcc', # snow and ice
'f7e084', # barren
'6f6f6f' # tundra
])
map.addLayer(
image=composite,
vis_params={'min':0, 'max':100, 'bands':"30,20,10"},
name='Landsat 7 TOA 2000')
map.addLayer(
image=cover,
vis_params={'palette': PALETTE, 'min':0, 'max':17},
name='MODIS land-cover',
visible=False)
# Multiple trainings with different numbers of points.
nums = [1000, 10000, 100000]
for num in nums:
# Extract random points.
points = ee.FeatureCollection.randomPoints(region, num, num, 1)
training = cover.addBands(composite).reduceToVectors(
reducer="mean",
geometry=points,
geometryType="centroid",
scale=30,
crs="EPSG:4326"
)
classifier = training.trainClassifier(
property_list=bands,
class_property="label",
classifier_name="FastNaiveBayes"
)
# Apply the classifier to the original composite.
out = composite.classify(classifier)
map.addLayer(
image=out,
vis_params={'palette': PALETTE, 'min':0, 'max':17},
name="{0} points".format(num),
visible=False
)
# Add the region that constrains the random sampling.
map.addLayer(
image=ee.Image().paint(ee.Feature(region), 0, 2),
name="Region"
)
In [ ]: